package com.itany.shop.controller;

import com.itany.shop.entity.Product;
import com.itany.shop.entity.User;
import com.itany.shop.exception.RequestParameterErrorException;
import com.itany.shop.exception.UserExistException;
import com.itany.shop.exception.UserNotExistException;
import com.itany.shop.factory.ObjectFactory;
import com.itany.shop.service.ProductService;
import com.itany.shop.service.UserService;
import com.itany.shop.util.PageInfo;
import com.itany.shop.vo.Cart;

import java.util.Date;
import java.util.List;
import java.util.Scanner;

/**
 * @author MissChen
 * @date 2024年03月29日 14:12
 */
public class UserController {
    private Scanner sc = new Scanner(System.in);
    private UserService userService = (UserService) ObjectFactory.getObject("userService");
    private ProductService productService = (ProductService) ObjectFactory.getObject("productService");

    public static void main(String[] args) {

        new UserController().showSystemMenu();


    }

    public void showSystemMenu() {
        System.out.print("请选择服务(1.注册 2.登录 3.查看商品列表)");
        int choice = sc.nextInt();
        switch (choice) {
            case 1:
                register();
                break;
            case 2:
                login();
                break;
            case 3:
                findAll();
                break;
        }
    }

    public void register() {
        System.out.print("请输入用户名:");
        String username = sc.next();
        System.out.print("请输入密码:");
        String password = sc.next();
        System.out.print("请输入手机号:");
        String phone = sc.next();
        System.out.print("请输入地址:");
        String address = sc.next();
        try {
            userService.register(username, password, phone, password);
            System.out.println("注册成功");
        } catch (RequestParameterErrorException e) {
            System.out.println("注册失败，失败原因:" + e.getMessage());
        } catch (UserExistException e) {
            System.out.println("注册失败，失败原因:" + e.getMessage());
        }
        showSystemMenu();
    }

    public void login() {
        System.out.print("请输入用户名:");
        String username = sc.next();
        System.out.print("请输入密码:");
        String password = sc.next();
        try {
            User user = userService.login(username, password);
            System.out.println("登录成功，欢迎您:" + user.getUsername());
            new MainController(user).showMenu();

        } catch (RequestParameterErrorException e) {
            System.out.println("登录失败，失败原因:" + e.getMessage());
        } catch (UserNotExistException e) {
            System.out.println("登录失败，失败原因:" + e.getMessage());
        }
        // showSystemMenu();
    }

    public void findAll() {
        // List<Product> products=productService.findAll();
        // if(!products.isEmpty()){
        //     System.out.println("商品编号\t名称\t价格");
        //     for(Product product:products){
        //         System.out.println(product.getId()+"\t"+product.getName()+"\t"+product.getPrice());
        //     }
        // }

        //分页查询
        System.out.print("请输入查询的第几页的数据:");
        int pageNo = sc.nextInt();

        System.out.print("请输入一页显示的数据条数:");
        int pageSize = sc.nextInt();
        PageInfo<Product> pageProduct = productService.findPage(pageNo, pageSize);
        System.out.println("商品编号\t名称\t价格");
        System.out.println("当前页:"+pageNo+"，上一页:"+pageProduct.getPrevPage()+",下一页:"+pageProduct.getNextPage()+",首页:"+pageProduct.getHeadPage()+",尾页:"+pageProduct.getFootPage()+",总条数："+pageProduct.getTotal());
        for (Product product : pageProduct.getList()) {
            System.out.println(product.getId() + "\t" + product.getName() + "\t" + product.getPrice());
        }
        System.out.println("请输入需要加入购物车中的商品编号:");
        int id=sc.nextInt();

    }
}
